package com.edais.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edais.util.RSAThirdParty.RSA;

public class ThirdPartyEnvUtil {
	private static final Log logger = LogFactory
			.getLog(ThirdPartyEnvUtil.class);

	public ThirdPartyEnvUtil() {
	}

	private static Properties props = new Properties();
	static {
		try {
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("thirdPartyconfig.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getValue(String key) {
		return props.getProperty(key);
	}

	public static void updateProperties(String key, String value) {
		props.setProperty(key, value);
	}

	/**
	 * 调用ThirdParty
	 * @param logInfo 日志
	 * @param map     
	 * @param url     /xx/yy
	 * @return
	 */
	public static Map<String, String> invoke(String logInfo, Map<String, String> map, String url) {
		String req = "";
		String res = "";
		Map<String, String> retMap = new HashMap<String, String>();
		retMap.put("rescode", "00004");
		retMap.put("resmsg_cn", "默认失败");
		try {
			url = props.getProperty("thirdPartyPath") + url;
			logger.info(logInfo + "调用第三方url" + url);
			logger.info(logInfo + "调用第三方Map" + JSONObject.fromObject(map).toString());
			req = "keyRandom=" + URLEncoder.encode(RSA.getPublick_Key_Encrypt(map), "utf-8");
			logger.info(logInfo + "调用第三方开始" + req);
			res = CommonUtil.sendPostByHttp(url, req);
			logger.info(logInfo + "调用第三方结束" + res);
			if ("".equals(res)) {
				logger.info(logInfo + "调用第三方返回值为空，请检查第三方地址或网络");
				retMap.put("resmsg_cn", "调用第三方返回值为空，请检查第三方地址或网络");
			} else {
				retMap = JsonUtil.getMapFromJsonString(res);
			}
		} catch (Exception e) {
			logger.error(logInfo + "调用第三方异常", e);
			retMap.put("rescode", "00004");
			retMap.put("resmsg_cn", "调用第三方异常" + e.toString());
		}
		return retMap;
	}
	
	
	/**
	 * 调用ThirdParty  返回String
	 * @param logInfo 日志
	 * @param map     
	 * @param url     /xx/yy
	 * @return
	 */
	public static String getThirdParty(String logInfo, Map<String, String> map, String url) {
		String req = "";
		String res = "";
		try {
			url = props.getProperty("thirdPartyPath") + url;
			logger.info(logInfo + "调用第三方url" + url);
			logger.info(logInfo + "调用第三方Map" + JSONObject.fromObject(map).toString());
			req = "keyRandom=" + URLEncoder.encode(RSA.getPublick_Key_Encrypt(map), "utf-8");
			logger.info(logInfo + "调用第三方开始" + req);
			res = CommonUtil.sendPostByHttp(url, req);
			logger.info(logInfo + "调用第三方结束" + res);
		} catch (Exception e) {
			logger.error(logInfo + "调用第三方异常", e);
		}
		return res;
	}
	
}
